package com.shop.shop.controller;


import com.shop.shop.entity.Commodity;
import com.shop.shop.service.CommodityService;
import com.shop.shop.utile.JWTUtils;
import com.shop.shop.utile.MapUtile;
import com.shop.shop.utile.Q;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/shop/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;


    /**
     * 推荐商品 加分页
     *
     * @return
     */
    @GetMapping("listTop")
    public Q list(MapUtile map) {
        map.setSpike(0);
        Map<String, Object> list = commodityService.findByAllTop(map);
        return Q.ok().data(list);
    }

    /**
     * 推荐商品 加分页
     *
     * @return
     */
    @GetMapping("list")
    public Q lists(MapUtile map) {
        Map<String, Object> list = commodityService.findSeartop(map);
        return Q.ok().data(list);
    }

    /**
     * 秒杀 商品
     *
     * @param map
     * @return
     */
    @GetMapping("spike")
    public Q spike(MapUtile map) {
        map.setSpike(1);
        Map<String, Object> list = commodityService.findByAllTop(map);
        return Q.ok().data(list);
    }

    /**
     * id查询商品
     */
    @GetMapping("/info/{id}")
    public Q info(@PathVariable("id") Integer id) {
        Map<String, Object> map = commodityService.findById(id);
        return Q.ok().data(map);
    }

    /**
     * 用户收藏
     *
     * @param map
     * @param request
     * @return
     */
    @GetMapping("/listUser")
    public Q listUser(MapUtile map, HttpServletRequest request) {
        Map<String, Object> map1 = JWTUtils.verify(request.getHeader("token"));
        map.setUserId(map1.get("id"));
        Map<String, Object> list = commodityService.listUser(map);
        return Q.ok().data(list);
    }

    /**
     * 分类查询 分页
     *
     * @param mapUtile
     * @param
     * @return
     */
    @GetMapping("getList")
    public Q getList(MapUtile mapUtile, String srName) {
        Map<String, Object> map = commodityService.getList(mapUtile, srName);
        return Q.ok().data(map);
    }
}

